package cn.damai.controller.web;

import cn.damai.model.User;
import cn.damai.user.service.UserService;
import cn.damai.util.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;


/**
 * 登录控制层
 */
@Controller
@RequestMapping("user")
public class LoginController {

    @Reference(url = DubboUtil.USER_URL,version = DubboUtil.VRESION,group = DubboUtil.GROUP)
    private UserService userService;

    @RequestMapping("to/login")
    public String toLogin(){
        return "login";
    }

    @PostMapping("login")
    public String loginByPassword(User user, HttpSession session, Model model) {
        boolean phoneExists=userService.checkPhoneExists(user.getPhone());
        String msg = null;
        if(phoneExists){
            //判断密码对不对
            User u=userService.queryByPhone(user.getPhone());
//            MD5 m =new MD5();
            if(PasswordEncrypt.authenticatePassword(u.getPassword(),user.getPassword())){
                session.setAttribute("user",u);
                return "redirect:/";
            }else {
                // 密码错误
                msg = "密码错误";
            }
        }else {
            // 账号不存在
            msg = "账号不存在";
        }
        model.addAttribute("errMsg",msg);
        return "login";

    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        return "login";
    }


    @RequestMapping("to/register")
    public String toRegister(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        return "register";
    }

    /**
     * 注册验证
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("register")
    public String register(User user , Model model,HttpSession session){
        if(user.getPhone().length() != 11){
            model.addAttribute("msg","手机号格式不正确");
            return "register";
        }
        boolean exists = userService.checkPhoneExists(user.getPhone());
        if(exists){
            model.addAttribute("msg","账号已存在");
            return "register";
        }else {
            int row = userService.register(user);
            if(row==1){
                User u = userService.queryByPhone(user.getPhone());
                session.setAttribute("user",u);
                model.addAttribute("registerSuc","注册成功，正为您跳转首页");
                return "register";
            }else {
                model.addAttribute("msg","注册失败");
                return "register";
            }

        }

    }


    /**
     * 检查是否注册
     * @param phone
     * @return
     */
    @RequestMapping("phone/{phone}/exists")
    @ResponseBody
    public String checkPhoneExists(@PathVariable("phone") String phone){
        boolean exists = userService.checkPhoneExists(phone);
        if(exists){
            return "OK";
        }else {
            return "FALL";
        }
    }


}
